package com.aizhixin.lab.project.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.template.domain.ProjectRoleDomain;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.ProjectRole;
import com.aizhixin.lab.project.template.service.ProjectRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping("/api/v1/project/Role")
@RestController
@Api(tags = "角色模块", value = "角色编辑")
public class ProjectRoleController {
    @Autowired
    private ProjectRoleService roleService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ProjectRoleDomain projectRole){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
             roleService.save(projectRole);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询所有角色", httpMethod = "POST", notes = "查询所有角色<br><br><b>lwq</b>")
    public ResponseEntity<?> findAll(@RequestHeader("Authorization") String token){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ProjectRoleDomain> roleList = roleService.findRoleList();
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,roleList);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除角色库的角色", httpMethod = "DELETE", notes = "删除角色库的角色<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,@RequestParam String id){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            roleService.delete(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,"删除成功");
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
//    @RequestMapping(value = "/syncRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "同步岗位到角色", httpMethod = "POST", notes = "同步岗位到角色<br><br><b>lwq</b>")
//    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token){
//        AccountDTO account = accountService.getUserByToken(token);
//        Map<String, Object> result = new HashMap<String, Object>();
//        if (account == null) {
//            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//        }
//        try{
//            roleService.syncRole();
//            result.put(ApiReturnConstants.SUCCESS, true);
//            result.put(ApiReturnConstants.DATA,"删除成功");
//        }catch (Exception e){
//            result.put(ApiReturnConstants.SUCCESS, false);
//            result.put(ApiReturnConstants.ERROR,e.getMessage());
//        }
//        return new ResponseEntity(result, HttpStatus.OK);
//    }
}
